<template>
  <div
    class="CarouselItem-container"
    ref="container"
    @mousemove="handleMove"
    @mouseleave="handleLeave"
  >
    <div class="carouse" :style="imgPosition" ref="img">
      <ImageLoader
        :src="carous.bigImg"
        :placeholder="carous.midImg"
        @load="showWords"
      />
    </div>
    <div class="title" ref="title">{{ carous.title }}</div>
    <div class="des" ref="des">{{ carous.description }}</div>
  </div>
</template>

<script>
import ImageLoader from "@/components/ImageLoader/index.vue";
export default {
  components: {
    ImageLoader,
  },
  props: ["carous"],
  data() {
    return {
      titleWidth: 0, //title宽度
      desWidth: 0, //des宽度
      parentSize: 0, //父级容器
      imgSize: 0, //图片容器
      mouseX: 0,
      mouseY: 0,
    };
  },
  computed: {
    imgPosition() {
      //得到图片坐标
      if (!this.imgSize || !this.parentSize) {
        return;
      }
      const extraWidth = this.imgSize.width - this.parentSize.width; // 多出的宽度
      const extraHeight = this.imgSize.height - this.parentSize.height; //多出的高度
      const left = (-extraWidth / this.parentSize.width) * this.mouseX;
      const top = (-extraHeight / this.parentSize.height) * this.mouseY;
      return {
        transform: `translate(${left}px, ${top}px)`,
      };
    },
    center() {
      return {
        x: this.parentSize.width / 2,
        y: this.parentSize.height / 2,
      };
    },
  },
  mounted() {
    this.titleWidth = this.$refs.title.clientWidth;
    this.desWidth = this.$refs.des.clientWidth;
    this.setSize();
    this.mouseX = this.center.x;
    this.mouseY = this.center.y;
    window.addEventListener("resize", this.setSize);
  },
  destoryed() {
    window.removeEventListener("resize", this.setSize);
  },
  methods: {
    showWords() {
      // 显示文字
      this.$refs.title.style.opacity = 1;
      this.$refs.title.style.width = 0;
      this.$refs.title.clientWidth; // 强制让浏览器渲染一次
      this.$refs.title.style.transition = "1s";
      this.$refs.title.style.width = this.titleWidth + "px";

      this.$refs.des.style.opacity = 1;
      this.$refs.des.style.width = 0;
      this.$refs.des.clientWidth;
      this.$refs.des.style.transition = "2s 1s";
      this.$refs.des.style.width = this.desWidth + "px";
    },
    setSize() {
      this.parentSize = {
        width: this.$refs.container.clientWidth,
        height: this.$refs.container.clientHeight,
      };
      this.imgSize = {
        width: this.$refs.img.clientWidth,
        height: this.$refs.img.clientHeight,
      };
    },
    handleMove(e) {
      const rect = this.$refs.container.getBoundingClientRect();
      this.mouseX = e.clientX - rect.left;
      this.mouseY = e.clientY - rect.top;
    },
    handleLeave() {
      this.mouseX = this.center.x;
      this.mouseY = this.center.y;
    },
  },
};
</script>

<style lang="less" scoped>
@import "~@/styles/var.less";
.CarouselItem-container {
  width: 100%;
  height: 100%;
  position: relative;
  color: #fff;
  overflow: hidden;

  .carouse {
    position: absolute;
    width: 110%;
    height: 110%;
    z-index: -1;
    left: 0;
    top: 0;
    transition: 0.3s;
  }
  .title,
  .des {
    position: absolute;
    letter-spacing: 3px;
    text-shadow: 1px 0 0 rgba(0, 0, 0, 0.5), -1px 0 0 rgba(0, 0, 0, 0.5),
      0 1px 0 rgba(0, 0, 0, 0.5), 0 -1px 0 rgba(0, 0, 0, 0.5);
    color: #fff;
    left: 50px;
    overflow: hidden;
    opacity: 0;
    white-space: nowrap;
    color: lighten(@gray, 20%);
  }
  .title {
    top: calc(50% - 40px);
    font-size: 2em;
  }
  .des {
    top: calc(50% + 30px);
    font-size: 1.5em;
  }
}
</style>